<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请填写用户昵称查询搜索"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="isLeader">
        <el-radio-group v-model="queryParams.isLeader" size="small" @input="getList">
          <el-radio v-for="(value, key) in $msg('isLeader')" :key="key" :label="key" border>{{ value }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <!-- <el-form-item label="入驻时间">
        <el-date-picker
          v-model="dateRange"
          type="datetimerange"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
          align="right" />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="wxList" border>
      <el-table-column label="用户序号" align="center" prop="userId" />
      <el-table-column label="用户昵称" align="center" prop="nickName" />
      <el-table-column label="用户身份" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isLeader"
            active-value="1"
            inactive-value="0"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="头像地址" align="center" prop="avatar">
        <template slot-scope="scope">
          <el-image :src="scope.row.avatar" class="table-column_image" />
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>
<script>
import { listWx, updateWx, exportWx } from "@/api/business/user"
export default {
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 用户信息表格数据
      wxList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickName: undefined,
        isLeader: '0',
        startTime: undefined,
        endTime: undefined,
      },
      dateRange: [],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
    }
  },
  watch: {
    dateRange: {
      handler(list) {
        if (list && list.length > 0) {
          this.queryParams.startTime = list[0]
          this.queryParams.endTime = list[1]
        } else {
          this.queryParams.startTime = undefined
          this.queryParams.endTime = undefined
        }
      },
      deep: true,
      immediate: true,
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询用户信息列表 */
    getList() {
      this.loading = true
      listWx(this.queryParams).then(res => {
        this.wxList = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 用户身份修改
    handleStatusChange(row) {
      this.$confirm(`确认要将 "${row.nickName}" 的用户身份变更为${row.isLeader === '1' ? '团长' : '团员'}吗？`, "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return updateWx(row)
      }).then(() => {
        this.msgSuccess('修改成功')
        this.getList()
      }).catch(() => {
        row.isLeader = row.isLeader === "0" ? "1" : "0"
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有用户信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportWx(queryParams)
        }).then(response => {
          this.download(response.msg)
        }).catch(function() {})
    },
  }
}
</script>
<style lang="scss" scoped>
.table-column_image {
  width: 40px;
  height: 40px;
  border-radius: 50%;
}
</style>
<style scoped>
.app-container /deep/ .el-radio--small.is-bordered {
  margin: 0 5px;
}
.table-column_image /deep/ .el-image__error {
  font-size: 8px;
}
</style>
